/**
 * 2010(c) Copyright Oceansoft Information System Co.,LTD. All rights reserved.
 * <p>
 * Compile: JDK 1.6+
 * <p>
 * 版权所有(C)：江苏欧索软件有限公司
 * <p>
 * 公司名称：江苏欧索软件有限公司
 * <p>
 * 公司地址：中国苏州科技城青山路1号
 * <p>
 * 网址: http://www.oceansoft.com.cn
 * <p>
 * 作者: 090922(陈伟)
 * <p>
 * 文件名: com.oceansoft.mobile.econsole.modules.apps.controller.AppController.java 
 * <p>
 * 类产生时间: 2014/4/29 0029 下午 22:27
 * <p>
 * 负责人: 090922(陈伟)
 * <p>
 * Email:javacspring@gmail.com
 * <p>
 * 所在组 : 掌上公安应用平台
 * <p>
 * 所在部门: 开发部--手持技术部
 * <p>
 * <p>
 */
package com.oceansoft.mobile.econsole.modules.channel.controller;

import com.oceansoft.mobile.econsole.modules.apps.entity.AppInfo;
import com.oceansoft.mobile.econsole.modules.base.controller.BaseController;
import com.oceansoft.mobile.econsole.modules.base.entity.Pager;
import com.oceansoft.mobile.econsole.modules.base.entity.Query;
import com.oceansoft.mobile.econsole.modules.base.entity.Result;
import com.oceansoft.mobile.econsole.modules.channel.entity.Channel;
import com.oceansoft.mobile.econsole.modules.channel.service.IChannelService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;

import static org.springframework.web.bind.annotation.RequestMethod.*;

/**
 * 应用App管理业务控制器
 * @author: jin.xu
 * @time: 2014/7/30
 */
@Controller
@RequestMapping("channel")
public class ChannelController extends BaseController {

    @Resource
    private IChannelService channelService;

    /**
     * 栏目列表
     * @param model 栏目列表
     * @return String
     */
    @RequestMapping(value = "index", method = GET)
    public String gotoChannelIndex(Query query, Model model) {
        query.setOrderField("sort");
        query.setOrderDirection("asc");
        Pager<Channel> pager = channelService.findChannelByPage(query);
        model.addAttribute("query", query);
        model.addAttribute("pager", pager);
        log("查看栏目列表");
        return "channel/channel-list";
    }

    /**
     * 列表分页显示
     *
     * @param query 分页查询条件
     * @param model 数据Model
     * @return 页面地址
     */
    @RequestMapping(value = "list", method = POST)
    public String loadPageByCondition(Query query, Model model) {
        Pager<Channel> pager = channelService.findChannelByPage(query);
        model.addAttribute("query", query);
        model.addAttribute("pager", pager);
        log(String.format("查看栏目列表第%d页", query.getPageNum()));
        return "channel/channel-list";
    }

    /**
     * 保存新增栏目
     */
    @ResponseBody
    @RequestMapping(value = "add", method = POST)
    public Result addChannel(Channel channel, Model model) {
        log(String.format("新增栏目[ %s ]，ID=%d", channel.getTitle(), channel.getId()));
        return channelService.save(channel);
    }

    /**
     * 进入应用新增或查看应用页面
     *
     * @return 新增页面URL
     */
    @RequestMapping(value = "view/{channel_id}", method = GET)
    public String viewChannel(@PathVariable("channel_id") int channelId, Model model) {
        if (0 == channelId) {//新增
            Channel channel = new Channel();
            channel.setId(0);
            model.addAttribute("channel", new AppInfo());
            return "channel/channel-add";
        } else {
            Channel channel = channelService.findChannelById(channelId);
            model.addAttribute("channel", channel);
            return "channel/channel-edit";
        }
    }

    /**
     * 更新栏目信息
     *
     * @param channel
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "edit", method = POST)
    public Result editChannel(Channel channel) {
        channelService.update(channel);
        log(String.format("更新栏目信息[ %s ]，ID=%d", channel.getTitle(), channel.getId()));
        return new Result();
    }

    /**
     * 删除栏目信息
     * @param channelId
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "del/{channel_id}", method = DELETE)
    public Result delChannel(@PathVariable("channel_id") int channelId) {
        channelService.deleteChannelById(channelId);
        log(String.format("删除栏目信息，ID=%d", channelId));
        return new Result();
    }
}
